<template>
   <el-button @click="handleAdd" v-if="$filters.auth($filters.PrivilegeType.Add)" class="button-header"  type="primary" :icon="Plus">添加顾问</el-button>
   <el-form inline :model="query" class="filter" label-width="100px">
    <el-form-item label="顾问名姓名：">
      <el-col :span="22">
        <el-input v-model="query.name" placeholder="请输入顾问姓名" />
      </el-col>
    </el-form-item>
    <el-form-item label="顾问手机号：">
      <el-col :span="22">
        <el-input v-model="query.phone" placeholder="请输入顾问手机号" />
      </el-col>
    </el-form-item>
    <el-form-item class="filter-actions">
          <el-button class="button-small" type="primary" @click="handleSearch">筛选</el-button>
          <el-button type="text" class="button-reset" @click="handleClear">重置筛选条件</el-button>
    </el-form-item>
   </el-form>
   <grid url="example/adviser/list" :params="query" ref="datalist">
      <el-table-column prop="name" label="姓名">
        <template #default="scope">
          <div class="thumbnail">
            <el-avatar :src=" commonStore.setting.fileDomain + scope.row.thumbnail" />
          <span>{{scope.row.name}}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="phone" label="手机号" />
      <el-table-column label="操作" align="right" width="200"  v-if="$filters.auth($filters.PrivilegeType.Edit) || $filters.auth($filters.PrivilegeType.Delete)">
        <template #default="scope">
          <el-button type="text" v-if="$filters.auth($filters.PrivilegeType.Edit)" @click="handleEdit(scope.row)">编辑</el-button>
          <el-popconfirm :width="300" v-if="$filters.auth($filters.PrivilegeType.Delete)" class="btn-popconfirm" @confirm="handleDelete(scope.row.id)" confirmButtonText="确认" cancelButtonText="取消" title="确定要删除该顾问吗？">
            <template #reference>
              <el-button type="text" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
   </grid>
   <el-dialog append-to-body v-model="postForm.dialogVisible" :title="postForm.title" width="30%">
    <el-form ref="postFormRef" label-width="150px" class="ruleForm" :model="postForm.form" :rules="postForm.rules">
      <el-form-item label="顾问姓名：" prop="username">
          <el-col :span="22">
            <el-input v-model="postForm.form.name" maxlength="20" :disabled="postForm.form.id > 0" show-word-limit placeholder="最多可输入20个字符" />
          </el-col>
      </el-form-item>
      <el-form-item label="手机号：" prop="phone">
          <el-col :span="22">
            <el-input v-model="postForm.form.phone" maxlength="11" :disabled="postForm.form.id > 0"  />
          </el-col>
      </el-form-item>
      <el-form-item label="密码：" :key="2" :prop="(postForm.form.id > 0 && !postForm.form.password) ? 'empty' : 'password'">
          <el-col :span="22">
            <el-input v-model="postForm.form.password" type="password" autocomplete="new-password" maxlength="20" show-word-limit placeholder="最多可输入20个字符" />
          </el-col>
      </el-form-item>
      <el-form-item label="头像：" prop="thumbnail">
        <choose-file tips="建议尺寸200x200像素" :limit="1" v-model="postForm.form.thumbnail" />
      </el-form-item>
    </el-form>
    <template #footer>
        <span class="dialog-footer">
          <el-button @click="postForm.dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSave(postFormRef)">确定</el-button>
        </span>
    </template>
   </el-dialog>
</template>
<script lang="ts" setup>
import {Plus} from '@element-plus/icons-vue'
import {ref,onMounted} from 'vue'
import { ExampleAdviserSave,ExampleAdviserDelete } from '@/api/example'
import {ElForm, ElMessage} from 'element-plus'
import Grid from '@/components/Grid.vue'
import ChooseFile from '@/components/ChooseFile.vue'
import { useCommonStore } from '@/stores/common'
const commonStore = useCommonStore()
const query = ref<AdviserQuery>({
  name: '',
  phone: ''
})
const postForm = ref<DialogForm<AdviserEdit>>({
  title:'添加顾问',
  dialogVisible: false,
  form: {
    id: 0,
    name: '',
    phone: '',
    password:'',
    thumbnail:''
  },
  rules: {
    name: [{ required: true, message: '请输入顾问名称', trigger: 'blur' }],
    password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
    thumbnail: [{ required: true, message: '请上传头像', trigger: 'change' }]
  }
})

type FormInstance = InstanceType<typeof ElForm>
const postFormRef = ref<FormInstance>()
const datalist = ref<InstanceType<typeof Grid>>()
/**
 * 添加顾问
 */
const handleAdd = ():void => {
  postForm.value.form =  {
    id: 0,
    name: '',
    phone: '',
    password:'',
    thumbnail:''
  }
  postForm.value.dialogVisible = true
  postForm.value.title ='添加顾问'
}
/**
 * 编辑顾问
 */
const handleEdit = (row: AdviserView):void => {
  postForm.value.form =  {
    id: row.id,
    name: row.name,
    password: '',
    phone:row.phone,
    thumbnail: row.thumbnail
  }

  postForm.value.dialogVisible = true
  postForm.value.title ='编辑顾问'
}
/**
 * 搜索
 */
const handleSearch = ():void => {
  datalist.value?.loadData()
}
/**
 * 清空
 */
const handleClear = ():void => {
  query.value.name = ''
  query.value.phone = ''
  datalist.value?.loadData()
}
const handleSave = (formEl: FormInstance | undefined): void => {
  if (!formEl) return
  formEl.validate((valid) => {
    if(valid) {
      ExampleAdviserSave(postForm.value.form).then((res:ServiceResponse<any>) => {
        if(res.success) {
          ElMessage.success('保存成功')
          datalist.value?.loadData()
          postForm.value.dialogVisible = false
        } else  ElMessage.error(res.msg)
      })
    }
  })
}
const handleDelete = (id:number):void => {
  ExampleAdviserDelete(id).then(((res:ServiceResponse<any>) => {
    if(res.success) {
          ElMessage.success('删除成功')
          datalist.value?.loadData()
        } else  ElMessage.error(res.msg)
  }))
}
onMounted(() => {
   if(!commonStore.setting.fileDomain)
      commonStore.getSetting()
})
</script>
<style lang="scss" scoped>
@import "@/assets/styles/mixin.scss";
@import "@/assets/styles/tablePage.scss";
.thumbnail {
  display: flex;
  align-items:center;
  span {
    margin-left: 10px;
  }
}
</style>